<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>参与投票</title>
<link type="text/css" rel="stylesheet" href="css/style.css" />
	<script src="http://47.106.66.89:8080/js/vue.min.js"></script>
	<script src="http://47.106.66.89:8080/js/axios.min.js"></script>
	<link  href="http://47.106.66.89:8080/js/ele-2.15.7/theme-chalk/index.css" rel="stylesheet">
	<script src="http://47.106.66.89:8080/js/ele-2.15.7/index.js"></script>
	<script src="./js/vote.js"></script>

</head>
<body>
<div id="app">

	<vote  >

		<div id="vote" class="wrap">
			<h2>参与投票</h2>
			<ul class="list">
				<li>
					<h4>{{vote.vname}}</h4>
					<p class="info">共有 {{vote.items.length}} 个选项，已有 {{vote.unum}} 个网友参与了投票。</p>
					<form method="post" action="Vote!save.action">
						<input type="hidden" name="entityId" value="103"/>
						<ol>

							<li v-for="i in vote.items">
								<input :type="vote.vtype=='单选'?'radio':'checkbox'" name="options"
									   :value="i.id" v-model="iids"  />
								{{i.iname}}
							</li>


						</ol>
						<p class="voteView">
							<input @click.prevent="commit" type="image" src="images/button_vote.gif" />
							<a :href="'view.html?id='+ vote.id"><img src="images/button_view.gif" /></a></p>
					</form>
				</li>
			</ul>
		</div>

	</vote>


</div>


<script>
	var v = new Vue({
		el : "#app",
		data : {
			vote : {items: []},
			iids: [],
			records: []

		},
		created() {
			axios.get("/vote/vote/find" + location.search).then(res => {
				this.vote = res.data;
			})
		},
		methods : {
			commit() {
				let vid=location.search.substring("?id=".length);
				if(Array.isArray(this.iids) == false){
					//将数字转为 一个元素的数组
					this.iids = [this.iids];
				}
				this.iids.forEach(iid=>{
					let record = {
						vid:vid,
						iid:iid
					};
					this.records.push(record);
				})

				axios.post("/vote/record/commit",this.records).then(res => {
					if(res.data.code == 1){
						//查看结果
						location.href = "view.html"+location.search;
					}
					else{
						this.$alert(res.data.msg);
					}
				})

			},

		}
	})
</script>

</body>
</html>
